using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using XNAPlatformer.Source.Weapons;

namespace XNAPlatformer.Source.Characters
{
    public class Player
    {
        //Main player texture, check documentation for proper format
        private Texture2D texture;
        private Weapon weapon;
        private Vector2 position;
        private float velocity;
        private float downVelocity;
        private bool isJumping;
        private bool jumpEnabled;
        //Basically sets height of jump, the higher the jump Counter the higher the jump
        private int jumpCounter;
        private bool isDead;
        private Vector2 direction;
        private bool facingForward;
        private bool isProne;

        //variable used by level to determine if map should move or not
        private bool moveMap;

        private bool aIsPressed;
        private bool xIsPressed;


        //Used for ground collision
        private int feetHeight;
        //Used for left/right collisions (walls, ledges, etc.)
        private int topHeight;

        //These contain pixel data for different sections of the texture; 
        private Color[] textureColTop;
        private Color[] textureColBottom;
        private Color[] textureWhole;


        #region Accessors
        public bool MoveMap
        {
            get
            {
                return moveMap;
            }
            set
            {
                moveMap = value;
            }
        }

        public bool IsDead
        {
            get
            {
                return isDead;
            }
            set
            {
                isDead = value;
            }
        }
        #endregion

        public Player(ContentManager content, Vector2 position)
        {
            this.texture = content.Load<Texture2D>("Content\\Textures\\Characters\\tempPic");
            this.position = position;
            velocity = 3.0f;
            downVelocity = 4.0f;
            jumpCounter = 0;
            isJumping = false;
            feetHeight = 55;
            topHeight = texture.Height - 30;
            weapon = new Weapon(content);

            textureWhole = new Color[texture.Width * texture.Height];
            textureColTop = new Color[(texture.Height - topHeight) * texture.Width];
            textureColBottom = new Color[(texture.Height - feetHeight)];
            texture.GetData(0, new Rectangle(0, 0, texture.Width, texture.Height - topHeight), textureColTop, 0, (texture.Height - topHeight) * texture.Width);
            texture.GetData(0, new Rectangle((int)(texture.Width / 2.0f), feetHeight, 1, texture.Height - feetHeight), textureColBottom, 0, (texture.Height - feetHeight));
            texture.GetData(textureWhole);
            
            isDead = false;

        }

        public void Update(GameTime gameTime, GamePadState gamePad)
        {
            moveMap = false;

            direction = gamePad.ThumbSticks.Left;

            if (direction.X < 0)
            {
                facingForward = false;
            }
            else if (direction.X > 0)
            {
                facingForward = true;
            }

            if (Math.Abs(direction.X) < .2 && direction.Y < 0)
            {
                isProne = true;
            }
            else
            {
                isProne = false;
            }


            if (!isProne)
            {
                position.X += gamePad.ThumbSticks.Left.X * velocity;
            }

            


            #region Movement Checks (??Could be better??)
            
            if (XNAPlatformer.IntersectPixels(new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height - topHeight), textureColTop,
                new Rectangle(GetFocus(), 0, 1280, 480), GetAlpha()) == true)
            {
                position.X -= gamePad.ThumbSticks.Left.X * velocity;
            }
            

            

            if (position.X > 640 * (2.0f / 3.0f) && XNAPlatformer.currentLevel.levelEnd == false)
            {
                position.X = 640 * (2.0f / 3.0f);
                moveMap = true;

            }
            else if (position.X > 640 * (2.0f / 3.0f) && XNAPlatformer.currentLevel.levelEnd == true && GetFocus() >= -637)
            {
                position.X = 640 * (2.0f / 3.0f);
                moveMap = true;
            }
            else if (position.X < 0)
            {
                position.X = 0;
            }
            else if (position.X > 640 - texture.Width)
            {
                position.X = 640 - texture.Width;
            }
            #endregion

            #region Jumping logic

            

            if (gamePad.Buttons.A == ButtonState.Pressed && aIsPressed == false && jumpEnabled == true)
            {
                aIsPressed = true;
                isJumping = true;

            }
            else if (gamePad.Buttons.A == ButtonState.Released)
            {
                aIsPressed = false;
            }
            
            if (isJumping == true)
            {
                jumpCounter++;
                position.Y -= downVelocity * 2;

                if (jumpCounter > 25)
                {
                    jumpCounter = 0;
                    isJumping = false;
                }
            }
            #endregion

            #region Shooting
            if (gamePad.Buttons.X == ButtonState.Pressed && xIsPressed == false)
            {
                xIsPressed = true;

                if (direction == Vector2.Zero && facingForward == true)
                {
                    direction = new Vector2(1, 0);
                }
                else if (direction == Vector2.Zero && facingForward == false)
                {
                    direction = new Vector2(-1, 0);
                }
                else if (isProne == true && facingForward == true)
                {
                    direction = new Vector2(1, 0);
                }
                else if (isProne == true && facingForward == false)
                {
                    direction = new Vector2(-1, 0);
                }


                //Should be this weapon.Fire(direction);
                XNAPlatformer.activeBullets.Add(new Bullet(position + new Vector2(70,20), direction, weapon.BulletTexture, weapon.BulletVelocity));
                
            }
            }
            else if (gamePad.Buttons.X == ButtonState.Released)
            {
                xIsPressed = false;
            }
            #endregion

            #region Floor Collision
            GetHeight();
            #endregion


            #region Enemy Collision Checks
            foreach (Bullet bullet in XNAPlatformer.enemyBullets)
            {
                if (XNAPlatformer.IntersectPixels(bullet.BoundingRectangle(), bullet.Colors, new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height), textureWhole) == true)
                {
                    isDead = true;
                    bullet.IsDead = true;
                }
            }

            foreach (Enemy enemy in XNAPlatformer.enemies)
            {
                if (XNAPlatformer.IntersectPixels(enemy.BoundingRectangle(), enemy.Colors, new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height), textureWhole) == true)
                {
                    isDead = true;
                    enemy.IsDead = true;
                }
            }
            #endregion


        }

        public void Draw(GameTime gameTime, SpriteBatch spritebatch)
        {

            Color color = Color.White;

            if (isDead)
            {
                color = Color.Red;
            }
            spritebatch.Draw(texture, position, color);
            
        }

        public void GetHeight()
        {
          GoDown();
        }

        public void GoDown()
        {
            if (XNAPlatformer.IntersectPixels(new Rectangle((int)position.X + (int)(texture.Width / 2.0f), (int)position.Y + feetHeight, 1, texture.Height - feetHeight), textureColBottom,
                new Rectangle(GetFocus(), 0, 1280, 480), GetAlpha()) == false)
            {
                position.Y += downVelocity;
                jumpEnabled = false;

            }
            else
            {
                
                jumpEnabled = true;
                GoUp();
            }


        }

        public void GoUp()
        {
            if (XNAPlatformer.IntersectPixels(new Rectangle((int)position.X + (int)(texture.Width / 2.0f), (int)position.Y + feetHeight, 1, texture.Height - feetHeight), textureColBottom,
                new Rectangle(GetFocus(), 0, 1280, 480), GetAlpha()) == true && XNAPlatformer.IntersectPixels(new Rectangle((int)position.X + (int)(texture.Width / 2.0f), (int)position.Y + feetHeight - (int)downVelocity, 1, texture.Height - feetHeight), textureColBottom,
                new Rectangle(GetFocus(), 0, 1280, 480), GetAlpha()) == true && XNAPlatformer.IntersectPixels(new Rectangle((int)position.X + (int)(texture.Width / 2.0f), (int)position.Y + feetHeight - (int)downVelocity * 5, 1, texture.Height - feetHeight), textureColBottom,
                new Rectangle(GetFocus(), 0, 1280, 480), GetAlpha()) == false)
            {
                position.Y -= downVelocity;
            }
        }

        public Color[] GetAlpha()
        {
            return XNAPlatformer.currentLevel.alphaMapColors;
        }

        public int GetFocus()
        {
            return XNAPlatformer.currentLevel.GetCurrentFocus();
        }
    }
}
